Hanye官网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[id].vue 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. <template>
  2. <div>
  3. <div class="w-full h-[55px] sm:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-12">
  6. <!-- 加载中 -->
  7. <div
  8. class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  9. ></div>
  10. </div>
  11. <div v-else>
  12. <!-- 面包屑导航 -->
  13. <div class="max-w-full mb-6 xl:px-2 lg:px-2 md:px-4 px-4 mt-6">
  14. <div class="max-w-screen-2xl mx-auto">
  15. <nuxt-link
  16. :to="`${homepagePath}/`"
  17. class="justify-start text-white/60 text-base font-normal"
  18. >{{ t("common.breadcrumb.home") }}</nuxt-link
  19. >
  20. <span class="text-white/60 text-base font-normal px-2"> / </span>
  21. <nuxt-link
  22. :to="`${homepagePath}/products`"
  23. class="text-white/60 text-base font-normal"
  24. >{{ t("common.breadcrumb.products") }}</nuxt-link
  25. >
  26. <span class="text-white/60 text-base font-normal px-2"> / </span>
  27. <nuxt-link
  28. v-if="product?.category"
  29. :to="`${homepagePath}/products?category=${encodeURIComponent(product.category)}`"
  30. class="text-white/60 text-base font-normal"
  31. >{{ product.category }}</nuxt-link
  32. >
  33. <span class="text-white/60 text-base font-normal px-2"> / </span>
  34. <span class="text-white text-base font-normal">{{
  35. product?.title || product?.name
  36. }}</span>
  37. </div>
  38. </div>
  39. <!-- 产品详情内容 -->
  40. <div
  41. v-if="product"
  42. class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4"
  43. >
  44. <div class="max-w-screen-2xl mx-auto">
  45. <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-16">
  46. <!-- 左侧产品图片 -->
  47. <div
  48. class="flex flex-col gap-6 lg:sticky lg:top-24 self-start select-none"
  49. >
  50. <!-- 主图展示 -->
  51. <div
  52. class="bg-zinc-900 rounded-lg p-8 relative overflow-hidden group aspect-square"
  53. >
  54. <!-- 主图容器 - Swiper -->
  55. <Swiper
  56. :modules="[Navigation, Pagination, EffectFade, Thumbs]"
  57. :slides-per-view="1"
  58. :pagination="{
  59. clickable: true,
  60. dynamicBullets: true,
  61. dynamicMainBullets: 3,
  62. }"
  63. :navigation="true"
  64. :effect="'fade'"
  65. :fade-effect="{ crossFade: true }"
  66. :thumbs="{ swiper: thumbsSwiper }"
  67. class="w-full h-full product-swiper"
  68. @swiper="swiperInstance = $event"
  69. @slideChange="currentSlideIndex = $event.activeIndex"
  70. >
  71. <SwiperSlide
  72. v-for="(image, slideIndex) in [
  73. product.image,
  74. ...(product.gallery || []),
  75. ]"
  76. :key="slideIndex"
  77. >
  78. <div class="relative w-full h-full">
  79. <!-- 当前图片 -->
  80. <img
  81. :src="image"
  82. :alt="`${product.name} - ${t('products.image', {
  83. index: slideIndex + 1,
  84. })}`"
  85. class="w-full h-full object-contain rounded-lg transition-all duration-500"
  86. :class="{
  87. 'opacity-0': isSlideThumbnailLoading[slideIndex],
  88. 'opacity-100':
  89. !isSlideThumbnailLoading[slideIndex] &&
  90. !slideThumbnailErrors[slideIndex],
  91. }"
  92. @load="handleSlideImageLoad(slideIndex)"
  93. @error="handleSlideImageError(slideIndex)"
  94. />
  95. <!-- 加载状态 -->
  96. <div
  97. v-if="isSlideThumbnailLoading[slideIndex]"
  98. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 z-10"
  99. >
  100. <div
  101. class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  102. ></div>
  103. </div>
  104. <!-- 错误提示 -->
  105. <div
  106. v-if="slideThumbnailErrors[slideIndex]"
  107. class="absolute inset-0 flex items-center justify-center bg-red-900/50 z-20"
  108. >
  109. <div class="flex flex-col items-center gap-2">
  110. <span class="text-white">{{
  111. t("products.imageLoadError")
  112. }}</span>
  113. <button
  114. @click.stop="retryLoadSlideImage(slideIndex)"
  115. class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors duration-300"
  116. >
  117. {{ t("products.retry") }}
  118. </button>
  119. </div>
  120. </div>
  121. </div>
  122. </SwiperSlide>
  123. </Swiper>
  124. <!-- 全局加载状态 - 仅在所有图片都未加载完成时显示 -->
  125. <div
  126. v-if="isInitialLoading"
  127. class="absolute inset-0 flex items-center justify-center bg-zinc-900/80 z-30"
  128. >
  129. <div
  130. class="animate-spin h-12 w-12 border-4 border-blue-500 rounded-full border-t-transparent"
  131. ></div>
  132. </div>
  133. </div>
  134. <!-- 缩略图列表 - Swiper -->
  135. <div
  136. class="relative bg-zinc-900 rounded-lg py-6 px-8 overflow-hidden product-thumbnail-container"
  137. >
  138. <Swiper
  139. :modules="[Navigation, Thumbs, FreeMode]"
  140. :slides-per-view="'auto'"
  141. :space-between="12"
  142. :free-mode="true"
  143. :watch-slides-progress="true"
  144. :navigation="{
  145. nextEl: '.swiper-thumb-next',
  146. prevEl: '.swiper-thumb-prev',
  147. }"
  148. class="thumbs-swiper"
  149. @swiper="thumbsSwiper = $event"
  150. >
  151. <SwiperSlide
  152. v-for="(image, index) in [
  153. product.image,
  154. ...(product.gallery || []),
  155. ]"
  156. :key="index"
  157. class="!w-16 !h-16 md:!w-20 md:!h-20 !flex-shrink-0 cursor-pointer transition-all duration-300 relative"
  158. >
  159. <div
  160. class="w-full h-full rounded-lg overflow-hidden thumbnail-fixed-size"
  161. :class="{
  162. 'ring-2 ring-blue-500 ring-offset-2 ring-offset-zinc-900':
  163. currentSlideIndex === index,
  164. 'hover:ring-1 hover:ring-blue-500/50 hover:ring-offset-1 hover:ring-offset-zinc-900':
  165. currentSlideIndex !== index,
  166. 'opacity-50':
  167. isSlideThumbnailLoading[index] ||
  168. slideThumbnailErrors[index],
  169. }"
  170. >
  171. <!-- 缩略图加载状态 -->
  172. <div
  173. v-if="isSlideThumbnailLoading[index]"
  174. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg z-10"
  175. >
  176. <div
  177. class="animate-spin h-4 w-4 border-2 border-blue-500 rounded-full border-t-transparent"
  178. ></div>
  179. </div>
  180. <!-- 缩略图遮罩 -->
  181. <div
  182. class="absolute inset-0 transition-all duration-300 rounded-lg"
  183. :class="{
  184. 'bg-black/30': currentSlideIndex === index,
  185. 'bg-black/0 hover:bg-black/20':
  186. currentSlideIndex !== index,
  187. }"
  188. ></div>
  189. <img
  190. :src="image"
  191. :alt="`${product.name} - ${t('products.image', {
  192. index: index + 1,
  193. })}`"
  194. class="w-full h-full object-cover transition-all duration-300 rounded-lg"
  195. :class="{
  196. 'opacity-0': isSlideThumbnailLoading[index],
  197. 'opacity-100':
  198. !isSlideThumbnailLoading[index] &&
  199. !slideThumbnailErrors[index],
  200. 'hover:scale-110': currentSlideIndex !== index,
  201. }"
  202. @load="handleSlideImageLoad(index)"
  203. @error="handleSlideImageError(index)"
  204. />
  205. <!-- 缩略图错误提示 -->
  206. <div
  207. v-if="slideThumbnailErrors[index]"
  208. class="absolute inset-0 flex items-center justify-center bg-red-900/50 rounded-lg"
  209. >
  210. <div class="flex flex-col items-center gap-1">
  211. <span class="text-white text-xs">{{
  212. t("products.error")
  213. }}</span>
  214. <button
  215. @click.stop="retryLoadSlideImage(index)"
  216. class="px-2 py-1 bg-blue-500 text-white text-xs rounded hover:bg-blue-600 transition-colors duration-300"
  217. >
  218. {{ t("products.retry") }}
  219. </button>
  220. </div>
  221. </div>
  222. </div>
  223. </SwiperSlide>
  224. </Swiper>
  225. <!-- 缩略图导航按钮 -->
  226. <button
  227. class="swiper-thumb-prev absolute top-1/2 left-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-blue-500 rounded-full transform -translate-y-1/2 transition-all duration-300"
  228. >
  229. <svg
  230. xmlns="http://www.w3.org/2000/svg"
  231. class="h-5 w-5 text-white"
  232. fill="none"
  233. viewBox="0 0 24 24"
  234. stroke="currentColor"
  235. >
  236. <path
  237. stroke-linecap="round"
  238. stroke-linejoin="round"
  239. stroke-width="2"
  240. d="M15 19l-7-7 7-7"
  241. />
  242. </svg>
  243. </button>
  244. <button
  245. class="swiper-thumb-next absolute top-1/2 right-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-blue-500 rounded-full transform -translate-y-1/2 transition-all duration-300"
  246. >
  247. <svg
  248. xmlns="http://www.w3.org/2000/svg"
  249. class="h-5 w-5 text-white"
  250. fill="none"
  251. viewBox="0 0 24 24"
  252. stroke="currentColor"
  253. >
  254. <path
  255. stroke-linecap="round"
  256. stroke-linejoin="round"
  257. stroke-width="2"
  258. d="M9 5l7 7-7 7"
  259. />
  260. </svg>
  261. </button>
  262. </div>
  263. </div>
  264. <!-- 右侧产品信息 -->
  265. <div class="flex flex-col gap-8">
  266. <!-- 产品名称 -->
  267. <div class="bg-zinc-900 rounded-lg p-6">
  268. <h1 class="text-white text-3xl font-medium mb-4">
  269. {{ product.title || product.name }}
  270. </h1>
  271. <div class="text-stone-400 text-lg leading-relaxed">
  272. {{ product.summary }}
  273. </div>
  274. </div>
  275. <!-- 产品参数 -->
  276. <div class="bg-zinc-900 rounded-lg p-6">
  277. <h2 class="text-white text-xl font-medium mb-6">
  278. {{ t("products.productSpecifications") }}
  279. </h2>
  280. <div class="grid grid-cols-1 gap-4">
  281. <div
  282. class="flex justify-between items-center py-2 border-b border-zinc-800"
  283. >
  284. <span class="text-stone-400">{{
  285. t("products.categoryTitle")
  286. }}</span>
  287. <span class="text-white font-medium">{{
  288. product.category
  289. }}</span>
  290. </div>
  291. <div
  292. class="flex justify-between items-center py-2 border-b border-zinc-800"
  293. >
  294. <span class="text-stone-400">{{
  295. t("products.usageTitle")
  296. }}</span>
  297. <span class="text-white font-medium">{{
  298. product.usage?.join(", ")
  299. }}</span>
  300. </div>
  301. <div
  302. v-if="product.capacities && product.capacities.length > 0"
  303. class="flex justify-between items-center py-2"
  304. >
  305. <span class="text-stone-400">{{
  306. t("products.capacitiesTitle")
  307. }}</span>
  308. <span class="text-white font-medium">{{
  309. product.capacities?.join(" / ")
  310. }}</span>
  311. </div>
  312. </div>
  313. </div>
  314. <!-- 产品描述 -->
  315. <div
  316. v-if="product.description"
  317. class="bg-zinc-900 rounded-lg p-6"
  318. >
  319. <h2 class="text-white text-xl font-medium mb-6">
  320. {{ t("products.productDescription") }}
  321. </h2>
  322. <div
  323. class="text-stone-400 leading-relaxed space-y-4 prose prose-invert max-w-none"
  324. >
  325. {{ product.description }}
  326. </div>
  327. </div>
  328. <div class="bg-zinc-900 rounded-lg p-6">
  329. <h2 class="text-white text-xl font-medium mb-6">
  330. {{ t("products.detailedDescription") }}
  331. </h2>
  332. <div
  333. class="text-stone-400 leading-relaxed space-y-4 prose prose-invert max-w-none"
  334. >
  335. <ContentRenderer :value="product.content" />
  336. </div>
  337. </div>
  338. <!-- 相关产品 -->
  339. <div
  340. v-if="relatedProducts.length > 0"
  341. class="bg-zinc-900 rounded-lg p-6"
  342. >
  343. <h2 class="text-white text-xl font-medium mb-6">
  344. {{ t("products.relatedProducts") }}
  345. </h2>
  346. <div
  347. class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
  348. >
  349. <nuxt-link
  350. v-for="relatedProduct in relatedProducts"
  351. :key="relatedProduct.id"
  352. :to="`${homepagePath}/products/${relatedProduct.id}`"
  353. class="group"
  354. >
  355. <div
  356. class="bg-zinc-800 rounded-lg p-4 transition-all duration-300 hover:bg-zinc-700"
  357. >
  358. <div
  359. class="aspect-square mb-4 overflow-hidden rounded-lg"
  360. >
  361. <img
  362. :src="relatedProduct.image"
  363. :alt="relatedProduct.title || relatedProduct.name"
  364. class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-110"
  365. />
  366. </div>
  367. <h3
  368. class="text-white text-lg font-medium mb-2 line-clamp-2"
  369. >
  370. {{ relatedProduct.title || relatedProduct.name }}
  371. </h3>
  372. <p class="text-stone-400 text-sm line-clamp-2">
  373. {{ relatedProduct.summary }}
  374. </p>
  375. </div>
  376. </nuxt-link>
  377. </div>
  378. </div>
  379. </div>
  380. </div>
  381. </div>
  382. </div>
  383. </div>
  384. </ErrorBoundary>
  385. </div>
  386. </template>
  387. <script setup lang="ts">
  388. /**
  389. * 产品详情页面
  390. * 展示产品主图、参数和描述
  391. */
  392. import { useErrorHandler } from "~/composables/useErrorHandler";
  393. import { useRoute, useI18n, useAsyncData } from "#imports";
  394. import { queryCollection } from "#imports";
  395. import { ContentRenderer } from "#components";
  396. import { Swiper, SwiperSlide } from "swiper/vue";
  397. import {
  398. Navigation,
  399. Pagination,
  400. EffectFade,
  401. Thumbs,
  402. FreeMode,
  403. } from "swiper/modules";
  404. import type { Swiper as SwiperType } from "swiper";
  405. import "swiper/css";
  406. import "swiper/css/navigation";
  407. import "swiper/css/pagination";
  408. import "swiper/css/effect-fade";
  409. const { error, isLoading } = useErrorHandler();
  410. const route = useRoute();
  411. const { locale, t } = useI18n();
  412. const id = route.params.id as string;
  413. const swiperInstance = ref<SwiperType | null>(null);
  414. const thumbsSwiper = ref<SwiperType | null>(null);
  415. // 图片状态
  416. const currentSlideIndex = ref(0);
  417. const isInitialLoading = ref(true); // 初始加载状态
  418. const isSlideThumbnailLoading = ref<boolean[]>([]);
  419. const slideThumbnailErrors = ref<boolean[]>([]);
  420. // 滚动跟随相关
  421. const scrollContainer = ref<HTMLElement | null>(null);
  422. const isSticky = ref(false);
  423. const homepagePath = computed(() => {
  424. return locale.value === "zh" ? "" : `/${locale.value}`;
  425. });
  426. interface Product {
  427. id: string;
  428. name: string;
  429. usage: string[];
  430. capacities: string[];
  431. category: string;
  432. categoryId: string;
  433. description: string;
  434. summary: string;
  435. image: string;
  436. gallery: string[];
  437. body: string;
  438. content?: any;
  439. meta?: {
  440. series?: string[];
  441. name?: string;
  442. title?: string;
  443. image?: string;
  444. summary?: string;
  445. };
  446. title?: string;
  447. }
  448. /**
  449. * 使用queryCollection获取产品数据
  450. */
  451. const { data: productContent } = await useAsyncData(
  452. `product-${id}`,
  453. async () => {
  454. try {
  455. // 使用queryCollection从content目录获取数据
  456. const content = await queryCollection("content")
  457. .where("path", "LIKE", `/products/${locale.value}/${id}`)
  458. .first();
  459. return content;
  460. } catch (err) {
  461. console.error("Error fetching product content:", err);
  462. error.value = new Error(t("products.loadError"));
  463. return null;
  464. }
  465. }
  466. );
  467. /**
  468. * 获取分类信息
  469. */
  470. const { data: categoryContent } = await useAsyncData(
  471. `category-${productContent.value?.meta?.categoryId}`,
  472. async () => {
  473. if (!productContent.value?.meta?.categoryId) return null;
  474. try {
  475. const content = await queryCollection("content")
  476. .where(
  477. "path",
  478. "LIKE",
  479. `/categories/${locale.value}/${productContent.value.meta?.categoryId}`
  480. )
  481. .first();
  482. return content;
  483. } catch (err) {
  484. console.error("Error fetching category:", err);
  485. return null;
  486. }
  487. },
  488. {
  489. immediate: !!productContent.value?.meta?.categoryId,
  490. }
  491. );
  492. /**
  493. * 使用计算属性解析产品数据
  494. */
  495. const product = computed<Product | null>(() => {
  496. if (!productContent.value) return null;
  497. // 提取产品数据
  498. const meta = productContent.value.meta || {};
  499. return {
  500. id: id,
  501. name: String(meta.name || productContent.value.title || ""),
  502. title: String(productContent.value.title || meta.name || ""),
  503. usage: Array.isArray(meta.usage) ? meta.usage : [],
  504. capacities: Array.isArray(meta.capacities) ? meta.capacities : [],
  505. category: categoryContent.value?.title || "",
  506. categoryId: meta.categoryId || "",
  507. description: productContent.value.description || "",
  508. summary: String(meta.summary || ""),
  509. image: String(meta.image || ""),
  510. gallery: Array.isArray(meta.gallery) ? meta.gallery : [],
  511. body: productContent.value.body || "",
  512. content: productContent.value,
  513. meta: {
  514. series: Array.isArray(meta.series) ? meta.series : [],
  515. name: String(meta.name || ""),
  516. title: String(productContent.value.title || ""),
  517. image: String(meta.image || ""),
  518. summary: String(meta.summary || ""),
  519. },
  520. };
  521. });
  522. /**
  523. * 获取相关产品
  524. */
  525. const { data: relatedProductsContent } = await useAsyncData(
  526. `related-products-${id}`,
  527. async () => {
  528. try {
  529. // 获取产品列表
  530. const content = await queryCollection("content")
  531. .where("path", "LIKE", `/products/${locale.value}/%`)
  532. .all();
  533. return content;
  534. } catch (err) {
  535. console.error("Error fetching related products:", err);
  536. return [];
  537. }
  538. }
  539. );
  540. /**
  541. * 处理相关产品数据
  542. */
  543. const relatedProducts = computed(() => {
  544. if (!relatedProductsContent.value || !product.value) return [];
  545. // 获取当前产品的分类和系列
  546. const currentCategory = product.value.categoryId;
  547. const currentSeries = product.value.meta?.series || [];
  548. const currentProductId = id;
  549. return relatedProductsContent.value
  550. .filter((item: any) => {
  551. // 排除当前产品 - 多重检查确保排除
  552. if (item._path === `/products/${locale.value}/${id}`) return false;
  553. const meta = item.meta || {};
  554. if (meta.name === currentProductId) return false;
  555. const itemCategoryId = meta.categoryId || "";
  556. const itemSeries = Array.isArray(meta.series) ? meta.series : [];
  557. // 判断是否同类别或同系列
  558. const isSameCategory =
  559. currentCategory && itemCategoryId === currentCategory;
  560. const hasSameSeries =
  561. currentSeries.length > 0 &&
  562. itemSeries.some((series: string) => currentSeries.includes(series));
  563. // 返回同类别或同系列的产品
  564. return isSameCategory || hasSameSeries;
  565. })
  566. .map((item: any) => {
  567. const meta = item.meta || {};
  568. return {
  569. id: meta.name || "",
  570. name: meta.name || item.title || "",
  571. title: item.title || meta.name || "",
  572. image: meta.image || "",
  573. summary: meta.summary || "",
  574. category: meta.categoryId || "",
  575. series: Array.isArray(meta.series) ? meta.series : [],
  576. };
  577. })
  578. .slice(0, 6); // 最多显示6个相关产品
  579. });
  580. /**
  581. * 预加载下一张图片
  582. */
  583. function preloadNextImage(image: string) {
  584. currentSlideIndex.value =
  585. (currentSlideIndex.value + 1) % (product.value?.gallery?.length || 1);
  586. }
  587. /**
  588. * 处理图片加载完成
  589. */
  590. function handleSlideImageLoad(index: number) {
  591. isSlideThumbnailLoading.value[index] = false;
  592. slideThumbnailErrors.value[index] = false;
  593. // 检查是否所有图片都已加载
  594. const allImagesLoaded = isSlideThumbnailLoading.value.every(
  595. (status) => !status
  596. );
  597. if (allImagesLoaded) {
  598. isInitialLoading.value = false;
  599. }
  600. }
  601. /**
  602. * 处理图片加载错误
  603. */
  604. function handleSlideImageError(index: number) {
  605. isSlideThumbnailLoading.value[index] = false;
  606. slideThumbnailErrors.value[index] = true;
  607. }
  608. /**
  609. * 重试加载图片
  610. */
  611. function retryLoadSlideImage(index: number) {
  612. isSlideThumbnailLoading.value[index] = true;
  613. slideThumbnailErrors.value[index] = false;
  614. // 确定正确的图片URL
  615. const images = [product.value?.image, ...(product.value?.gallery || [])];
  616. const imageUrl = images[index];
  617. // 检查图片URL是否有效
  618. if (!imageUrl) {
  619. console.error("Invalid image URL:", index);
  620. slideThumbnailErrors.value[index] = true;
  621. isSlideThumbnailLoading.value[index] = false;
  622. return;
  623. }
  624. // 创建新的图片对象并设置超时
  625. const img = new Image();
  626. const timeoutId = setTimeout(() => {
  627. console.error("Image load timeout:", index);
  628. handleSlideImageError(index);
  629. }, 10000); // 10秒超时
  630. img.onload = () => {
  631. clearTimeout(timeoutId);
  632. console.log("Image loaded successfully:", index);
  633. handleSlideImageLoad(index);
  634. };
  635. img.onerror = (error) => {
  636. clearTimeout(timeoutId);
  637. console.error("Image load error:", { index, error });
  638. handleSlideImageError(index);
  639. };
  640. // 设置跨域属性
  641. img.crossOrigin = "anonymous";
  642. // 最后设置src以开始加载
  643. img.src = imageUrl;
  644. }
  645. // 页面加载时初始化状态
  646. onMounted(() => {
  647. // 初始化缩略图加载状态数组
  648. const galleryLength = (product.value?.gallery?.length || 0) + 1;
  649. isSlideThumbnailLoading.value = new Array(galleryLength).fill(true);
  650. slideThumbnailErrors.value = new Array(galleryLength).fill(false);
  651. // 设置初始加载状态
  652. isInitialLoading.value = true;
  653. // 预加载所有缩略图
  654. const images = [product.value?.image, ...(product.value?.gallery || [])];
  655. images.forEach((image, index) => {
  656. if (image) {
  657. const img = new Image();
  658. img.onload = () => handleSlideImageLoad(index);
  659. img.onerror = () => handleSlideImageError(index);
  660. img.src = image;
  661. }
  662. });
  663. console.log("Initialized thumbnail states:", {
  664. loading: isSlideThumbnailLoading.value,
  665. errors: slideThumbnailErrors.value,
  666. });
  667. // 添加滚动监听
  668. scrollContainer.value = document.querySelector(".max-w-screen-2xl");
  669. if (scrollContainer.value) {
  670. window.addEventListener("scroll", handleScroll, { passive: true });
  671. }
  672. });
  673. // 清理滚动监听
  674. onUnmounted(() => {
  675. if (scrollContainer.value) {
  676. window.removeEventListener("scroll", handleScroll);
  677. }
  678. });
  679. // 处理滚动事件
  680. function handleScroll() {
  681. if (!scrollContainer.value) return;
  682. const containerRect = scrollContainer.value.getBoundingClientRect();
  683. const scrollTop = window.scrollY || document.documentElement.scrollTop;
  684. // 当容器顶部距离视窗顶部小于100px时,启用sticky
  685. isSticky.value = containerRect.top < 100;
  686. }
  687. // SEO优化
  688. useHead(() => ({
  689. title: `${product.value?.name || "产品详情"} - Hanye`,
  690. meta: [
  691. {
  692. name: "description",
  693. content: product.value?.description || "产品详情页面",
  694. },
  695. ],
  696. }));
  697. </script>
  698. <style lang="scss" scoped>
  699. /* 隐藏滚动条但保持滚动功能 */
  700. .scrollbar-hide {
  701. -ms-overflow-style: none; /* IE and Edge */
  702. scrollbar-width: none; /* Firefox */
  703. }
  704. .scrollbar-hide::-webkit-scrollbar {
  705. display: none; /* Chrome, Safari and Opera */
  706. }
  707. /* 图片过渡动画 */
  708. .main-image {
  709. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  710. }
  711. /* 缩略图悬停效果 */
  712. .thumbnail-item {
  713. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  714. }
  715. .thumbnail-item:hover {
  716. transform: translateY(-2px);
  717. }
  718. /* 缩略图选中效果 */
  719. .thumbnail-item.selected {
  720. transform: scale(1.05);
  721. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  722. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  723. }
  724. /* 产品信息卡片效果 */
  725. .info-card {
  726. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  727. }
  728. .info-card:hover {
  729. transform: translateY(-2px);
  730. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  731. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  732. }
  733. /* 滚动跟随效果 */
  734. .lg\:sticky {
  735. position: sticky;
  736. top: 6rem; /* 96px */
  737. transition: all 0.3s ease;
  738. z-index: 10;
  739. max-height: calc(100vh - 6rem);
  740. overflow-y: auto;
  741. }
  742. @media (max-width: 1024px) {
  743. .lg\:sticky {
  744. position: relative;
  745. top: 0;
  746. max-height: none;
  747. }
  748. }
  749. /* Swiper 样式定制 */
  750. .product-swiper {
  751. :deep(.swiper-pagination-bullet) {
  752. background-color: white;
  753. opacity: 0.5;
  754. }
  755. :deep(.swiper-pagination-bullet-active) {
  756. opacity: 1;
  757. background-color: theme("colors.blue.500");
  758. }
  759. :deep(.swiper-button-next),
  760. :deep(.swiper-button-prev) {
  761. color: theme("colors.blue.500");
  762. background-color: rgba(0, 0, 0, 0.3);
  763. width: 36px;
  764. height: 36px;
  765. border-radius: 50%;
  766. &:after {
  767. font-size: 16px;
  768. font-weight: bold;
  769. }
  770. &:hover {
  771. background-color: theme("colors.blue.500");
  772. color: white;
  773. }
  774. }
  775. :deep(.swiper-button-disabled) {
  776. opacity: 0.35;
  777. cursor: auto;
  778. pointer-events: none;
  779. }
  780. :deep(.swiper-slide) {
  781. display: flex;
  782. align-items: center;
  783. justify-content: center;
  784. }
  785. }
  786. /* 缩略图Swiper样式 */
  787. .thumbs-swiper {
  788. overflow: visible;
  789. padding: 0.25rem;
  790. min-height: 6rem;
  791. :deep(.swiper-wrapper) {
  792. align-items: center;
  793. display: flex;
  794. min-height: 6rem;
  795. }
  796. :deep(.swiper-slide) {
  797. width: auto;
  798. height: auto;
  799. opacity: 0.7;
  800. transition: all 0.3s ease;
  801. display: flex;
  802. align-items: center;
  803. justify-content: center;
  804. }
  805. :deep(.swiper-slide-thumb-active) {
  806. opacity: 1;
  807. transform: scale(1.05);
  808. z-index: 1;
  809. }
  810. }
  811. /* 自定义缩略图导航按钮 */
  812. .swiper-thumb-next,
  813. .swiper-thumb-prev {
  814. &:focus {
  815. outline: none;
  816. }
  817. &.swiper-button-disabled {
  818. opacity: 0.3;
  819. cursor: default;
  820. background-color: rgba(0, 0, 0, 0.2);
  821. &:hover {
  822. background-color: rgba(0, 0, 0, 0.2);
  823. }
  824. }
  825. }
  826. /* 响应式调整 */
  827. @media (max-width: 768px) {
  828. .thumbs-swiper {
  829. padding: 0;
  830. :deep(.swiper-slide) {
  831. margin-right: 8px;
  832. }
  833. }
  834. /* 确保缩略图容器在移动设备上有足够空间 */
  835. .product-thumbnail-container {
  836. padding: 1rem;
  837. .swiper-thumb-prev {
  838. left: 0.5rem;
  839. width: 2rem;
  840. height: 2rem;
  841. }
  842. .swiper-thumb-next {
  843. right: 0.5rem;
  844. width: 2rem;
  845. height: 2rem;
  846. }
  847. }
  848. }
  849. /* 解决不同分辨率下缩略图大小问题 */
  850. .thumbnail-fixed-size {
  851. width: 100%;
  852. height: 100%;
  853. aspect-ratio: 1 / 1;
  854. position: relative;
  855. }
  856. </style>